package cn.kust.clock.repository.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @author YL
 * @data 2022/7/25 10:53
 **/
@Repository
public class LikeTravelsRedisRepository implements cn.kust.clock.repository.LikeTravelsRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public Boolean exists(Long id) {
        String key = KEY_LIKE_TRAVELS_ID_PREFIX + id;
        return redisTemplate.hasKey(key);
    }

    @Override
    public void saveEmptyValue(Long id) {
        String key = KEY_LIKE_TRAVELS_ID_PREFIX + id;
        redisTemplate.opsForValue().set(key, null, 30, TimeUnit.SECONDS);
    }

    @Override
    public void save(Integer LikeNum, Long typeId) {
        String key = KEY_LIKE_TRAVELS_ID_PREFIX + typeId;
        redisTemplate.opsForValue().set(key, LikeNum);
    }

    @Override
    public Integer getCountById(Long typeId) {
        String key = KEY_LIKE_TRAVELS_ID_PREFIX + typeId;
        Serializable result = redisTemplate.opsForValue().get(key);
        if (result == null) {
            return null;
        } else {
            return (Integer) result;
        }
    }
}
